package com.s10.ecloud.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.s10.ecloud.web.Constants;
import com.s10.ecloud.web.model.DeviceModel;
import com.s10.ecloud.web.model.DeviceType;
import com.s10.ecloud.web.model.EnterpriseUser;
import com.s10.ecloud.web.model.User;
import com.s10.ecloud.web.service.IDeviceService;

public class AddDeviceModelAction extends ActionSupport {
	@Resource
	private IDeviceService deviceService;

	@Action(value = "/toAddDeviceModel", results = { @Result(name = "success", location = "/page/ent/addDeviceModelPage.jsp") })
	public String toAddDeviceModel() throws Exception {
		this.setDeviceTypeList(this.deviceService.getDeviceType());
		return this.SUCCESS;
	}

	@Action(value = "/addDeviceModel", results = { @Result(name = "success", location = "/page/ent/index.jsp") })
	public String addDeviceModel() throws Exception {

		User currentUser = ((User) ActionContext.getContext().getSession()
				.get(Constants.SESSION_KEY_USER));

		// 1.输入check

		// 2.向DB中插入数据

		this.getDeviceModel().setUser((EnterpriseUser) currentUser);
		DeviceType type = new DeviceType();
		type.setId(this.getDeviceTypeId());
		this.deviceModel.setDeviceType(type);
		/*
		 * for (DeviceType type : this.getDeviceTypeList()) { if (type.getId()
		 * == this.getDeviceTypeId()) this.deviceModel.setDeviceType(type);
		 * break; }
		 */
		this.getDeviceModel().setState("0");

		deviceService.addDeviceModel(this.getDeviceModel());

		this.addActionMessage("添加成功");

		return this.SUCCESS;
	}

	private String functionCode;
	private int deviceTypeId;

	/**
	 * @return the deviceTypeId
	 */
	public int getDeviceTypeId() {
		return deviceTypeId;
	}

	/**
	 * @param deviceTypeId
	 *            the deviceTypeId to set
	 */
	public void setDeviceTypeId(int deviceTypeId) {
		this.deviceTypeId = deviceTypeId;
	}

	private DeviceModel deviceModel;

	private List<DeviceType> deviceTypeList;

	/**
	 * @return the deviceTypeList
	 */
	public List<DeviceType> getDeviceTypeList() {
		return deviceTypeList;
	}

	/**
	 * @param deviceTypeList
	 *            the deviceTypeList to set
	 */
	public void setDeviceTypeList(List<DeviceType> deviceTypeList) {
		this.deviceTypeList = deviceTypeList;
	}

	/**
	 * @return the deviceModel
	 */
	public DeviceModel getDeviceModel() {
		return deviceModel;
	}

	/**
	 * @param deviceModel
	 *            the deviceModel to set
	 */
	public void setDeviceModel(DeviceModel deviceModel) {
		this.deviceModel = deviceModel;
	}

	/**
	 * @return the functionCode
	 */
	public String getFunctionCode() {
		return functionCode;
	}

	/**
	 * @param functionCode
	 *            the functionCode to set
	 */
	public void setFunctionCode(String functionCode) {
		this.functionCode = functionCode;
	}

}
